<%-- 
    Document   : jspModel
    Created on : Feb 29, 2012, 4:11:44 AM
    Author     : 章哲原
--%>

<%@page import="com.cmti.commons.web.AuthenticationFilter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<%
    pageContext.setAttribute("contextPath", request.getContextPath());
    pageContext.setAttribute("loginUser", session.getAttribute(AuthenticationFilter.AUTH_INFO));
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <title>JSP Model Page</title>
        <link rel="stylesheet" href="${contextPath}/resources/css/easyui/easyui.css"/>
        <link rel="stylesheet" href="${contextPath}/resources/ueditor/themes/default/ueditor.css"//>
        <link rel="stylesheet" href="${contextPath}/resources/css/style.css"/>
        <link rel="stylesheet" href="${contextPath}/resources/css/icon.css"/>
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.form.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/datagrid-detailview.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/mask.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/js.js"></script>
        <script type="text/javascript" >
            var oldNode ;
            var nowNode ;
            $(document).ready(function(){
                
                $('#ratioList').treegrid({
                    title:'风险控制比率配置',
                    iconCls:'icon-tedit', //图标  
                    width:470,
                    height:500,
                    nowrap: true,
                    rownumbers: true,
                    animate:true,
                    collapsible:true,
                    singleSelect:true,
                    idField:'id',
                    treeField:'rcDesc',
                    url:'${contextPath}/config/risk/list?cloudId='+${loginUser.cloudId},
                    loadFilter:function(data){return data.risk},
                    columns:[[
                            {field:'rcDesc',title:'风控名称',width:320},
                            {field:'weight',title:'风控比率',width:100,editor:{type:'numberbox',options:{max:1,precision:2}},styler:function(value,rowData){
                                    if(rowData.pId ==0)
                                        return "background:#e3f2e1";
                                    else
                                        return "background:#fffddd";
                                }}
                        ]],
                    toolbar:[{
                            id:'editBt',
                            text:'修改风控比率',
                            iconCls:'icon-edit',
                            handler:function(){
                                var node = $('#ratioList').treegrid('getSelected');
                                nowNode = node;
                                if (node){
                                    $('#ratioList').treegrid('beginEdit',node.id);
                                    if(oldNode == node){
                                        oldNode = null;
                                    }
                                    if(oldNode != null){
                                        $('#ratioList').treegrid('endEdit',oldNode.id);
                                    }
                                    oldNode = node;
                                    $(".datagrid-editable-input:first").select();
                                }
                            }
                        },{
                            id:'cancelBt',
                            text:'取消',
                            iconCls:'icon-undo',
                            handler:function(){
                                if(oldNode != null){
                                    $('#ratioList').treegrid('cancelEdit',oldNode.id);
                                }
                            }
                        },{
                            text:'刷新',
                            iconCls:'icon-reload',
                            handler:function(){
                                $('#ratioList').treegrid('reload');
                            }
                        },{
                            id:'saveBt',
                            text:'保存修改',
                            iconCls:'icon-ok',
                            handler:function(){
                                $.messager.confirm('友情提示', '您确定要保存吗？', function(r){
                                    if(r){
                                        if(nowNode){
                                            $('#ratioList').treegrid('endEdit',nowNode.id);
                                        }
                                
                                        var data = $('#ratioList').treegrid('getData');
                                        var s = '';
                                        var sv = '';
                                        var total = 0;
                                        var sumflag=true;
                                        $.each(data,function(i,v){
                                            total += v.weight*1000;
                                            var sum = 0;
                                            if (s != ''){
                                                s += ',';
                                            } 
                                            if (sv != ''){
                                                sv += ',';
                                            }
                                            s+=v.id;
                                            sv += v.weight;
                                            $.each(v.children,function(ii,vv){
                                                sum +=vv.weight*1000;
                                                if (s != ''){
                                                    s += ',';
                                                } 
                                                if (sv != ''){
                                                    sv += ',';
                                                }
                                                s+=vv.id;
                                                sv += vv.weight;
                                            });
                                            if(sum != 1000){
                                                $.messager.alert('友情提示', '风控名称为"'+v.rcDesc+'"的子风控比率总和不等于1，无法保存，请修正！', 'error');
                                                sumflag = false;
                                                return false;
                                            }else{
                                                sumflag = true;
                                            }
                                        });
                                        if(!sumflag){
                                            return false;
                                        }
                                        if(total != 1000){
                                            $.messager.alert('友情提示', '风控比率总和不等于1，无法保存，请修正！', 'error');
                                            return false;
                                        }
                                        $("#addRiskForm").append("<input type='hidden' name='ratId' value='"+s+"' />");
                                        $("#addRiskForm").append("<input type='hidden' name='ratValue' value='"+sv+"' />");
                                        var upOptions = {//表单提交前的配置（jquery-form）
                                            url:"${contextPath}/config/risk/modify",//即action地址
                                            type:"POST",
                                            dataType : "json",
                                            //表单提交成功后执行
                                            success:function(updateData){//提交成功后执行的操作，addData（形参，可任意取名）为返回的json，
                                                if(updateData.ifUpdate){
                                                    $.messager.alert('友情提示','保存成功！','info');
                                                    //将角色Id移除，以免下次添加时重复添加
                                                    $('#addDialog').dialog('close');
                                                    $('#riskList').datagrid('reload');
                                                }else{
                                                    $.messager.alert("友情提示","保存失败，请稍后再试！","error");
                                                }
                                            },
                                            error:new jQueryAjaxErrorHandler()
                                        };
                                        $("#addRiskForm").ajaxSubmit(upOptions);//表单进行提交，带上option参数，提交成功后完成option里的动作
                                        $("#addRiskForm").resetForm();
                                        $("input[name=ratId]").remove();
                                        //移除uId隐藏域
                                        $("input[name=ratValue]").remove();
                                        $("input[name=cloudId]").remove();
                                    }
                                });
                            }
                        },'-',{
                            id:'passRisk',
                            text:'配置合格分数',
                            iconCls:'icon-wrench',
                            handler:function(){
                                $.ajax({
                                    url:"${contextPath}/config/risk/score",
                                    success:function(score){
                                        $("#nowPass").text(score.config.value);
                                    },
                                    error:new jQueryAjaxErrorHandler()
                                });
                                $("#passRiskDialog").dialog('open');
                            }
                        }],
                    onDblClickRow:function(){
                        var node = $('#ratioList').treegrid('getSelected');
                        nowNode = node ;
                        if (node){
                            $('#ratioList').treegrid('beginEdit',node.id);
                            if(oldNode == node){
                                oldNode = null;
                            }
                            if(oldNode != null){
                                $('#ratioList').treegrid('endEdit',oldNode.id);
                            }
                            oldNode = node;
                            $(".datagrid-editable-input:first").select();
                        }
                    },
                    onClickRow:function(){
                        if(oldNode != null){
                            $('#ratioList').treegrid('endEdit',oldNode.id);
                        }
                    }
                });
                
               
                $("#pass").validatebox({
                    required:true
                });
                $("#passRiskDialog").dialog({
                    title:'配置合格分数',
                    iconCls:'icon-page-white-wrench',
                    closed:true,
                    modal:true,
                    shadow:false,
                    width:260,
                    buttons:[{
                            text:'保存分数',
                            iconCls:'icon-save',
                            handler:function(){
                                $.messager.confirm('友情提示', '您确定要修改风险控制合格分数吗？', function(r){
                                    if(r){
                                        var mdOptions = {
                                            url:"${contextPath}/config/risk/passrisk",
                                            type:"POST",
                                            dataType : "json",
                                            beforeSubmit : function(){
                                                return $("#passRiskForm").form('validate');
                                            },
                                            //表单提交成功后执行
                                            success:function(updataData){
                                                if(updataData.result){
                                                    $.messager.alert('友情提示','分数修改成功！','info');
                                                    $('#passRiskDialog').dialog('close');
                                                }else{
                                                    $.messager.alert("友情提示","分数修改失败，请稍后再试！","error");
                                                }
                                            },
                                            error:new jQueryAjaxErrorHandler()
                                        };
                                        $("#passRiskForm").ajaxSubmit(mdOptions);
                                    }
                                });
                            }
                        },{
                            text:'取消',
                            handler:function(){
                                $('#passRiskDialog').dialog('close');
                            }
                        }],
                    onBeforeClose:function(){
                        $("#passRiskForm").clearForm();
                        $("#passRiskForm input").removeClass("validatebox-invalid");
                    }
                });
            });
        </script>
    </head>
    <body>
        <script type="text/javascript">
            $("body").mask({
                maskMsg:"正在初始化，请稍后...",
                timeout:1000,
                opacity:1
            });
        </script>
        <table id="ratioList"></table>
        <div class="normalpanel">
            <form id="addRiskForm"></form>
        </div>
        <div id="passRiskDialog" class="easyui-dialog">
            <div class="normalpanel">
                <form id="passRiskForm">
                    <input type="hidden" name="userId" value="${loginUser.id}"/>
                    <table style="margin-left: auto;margin-right: auto;border-spacing: 3px;">
                        <tr>
                            <td colspan="2">
                                <p style="text-indent:24px;">整机销售信息录入结束后计算风险控制分数，如果分数低于配置的合格分数，则自动审核不通过，需要人工对此整机销售信息进行审核。</p>
                            </td>
                        </tr>
                        <tr>
                            <td colspan ="2">
                                <p style="text-indent:24px;">当前合格分数为：<span id="nowPass" class="redFont"></span></p>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" width="70px" >
                                <span class="redFont">*</span>修改分数：
                            </td>
                            <td>
                                <input type="text" name="pass" id="pass" class="normaltext"/>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </body>
</html>
